//
//  CHChangePwdController.m
//  ITSI
//
//  Created by lili on 16/6/22.
//  Copyright © 2016年 陈浩. All rights reserved.
//

#import "CHChangePwdController.h"
#import "CHLoginViewController.h"
#import "DESCript.h"
@interface CHChangePwdController ()
       // 原密码
@property (weak, nonatomic) IBOutlet UITextField *pwdField;           // 新密码
@property (weak, nonatomic) IBOutlet UILabel *usernameLabel;

@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UITextField *newpwdField;
- (IBAction)changePwd:(id)sender;

- (IBAction)newBtnClick:(id)sender;
- (IBAction)sureBtnClick:(id)sender;

@end

@implementation CHChangePwdController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.title = @"修改密码";
    
    self.navigationController.navigationBar.hidden = NO;
    self.usernameLabel.text = (NSString *)[Util getValue:@"userflag"];
    self.sureBtn.layer.cornerRadius = 5;
    self.sureBtn.layer.masksToBounds = YES;
}

- (IBAction)changePwd:(id)sender {
    
    NSString *pwd =  (NSString *)[Util getValue:@"pwd"];
    
    if (_pwdField.text.length == 0 || _pwdField.text == nil) {
        [MBProgressHUD showTipMessageInView:@"原密码不能为空"];
        
    }else if(_newpwdField.text.length == 0 || _newpwdField.text == nil){
        [MBProgressHUD showTipMessageInView:@"新密码不能为空"];
        
    }else if(![_pwdField.text isEqualToString:pwd]){
        
        [MBProgressHUD showTipMessageInView:@"原密码不正确"];
        
    }else if(_newpwdField.text.length <= 6){
        
        [MBProgressHUD showTipMessageInView:@"密码必须由6位以上数字或字母组成"];
        
    }else if ([_newpwdField.text isEqualToString:pwd]){
        
        [MBProgressHUD showTipMessageInView:@"新密码不能与原密码相同"];
        
    }else{
        
        //        [MBProgressHUD showMessage:@"修改密码中..."];
        
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"userflag"] = [Globle getInstance].userInfoDic[@"userflag"];
        params[@"oldpass"] = [DESCript encrypt:self.pwdField.text encryptOrDecrypt:kCCEncrypt key:@"s(p7~;j^"];
        params[@"newpass"] = [DESCript encrypt:self.newpwdField.text encryptOrDecrypt:kCCEncrypt key:@"s(p7~;j^"];
        params[@"user"] = [Globle getInstance].user;
        NSString *sign = [DESCript encrypt:[Globle getInstance].userInfoDic[@"userflag"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
        params[@"sign"] = sign;
        [[Globle getInstance].service requestWithServiceName:@"lcipappupdatepass" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
            [MBProgressHUD hideHUD];
            
            NSLog(@"修改密码%@",result);
            if (result == nil) {
                
                [MBProgressHUD showError:@"修改失败"];
            }else{
                
                if ([result[@"code"] isEqualToString:@"1"]) {
                    
                    [MBProgressHUD showSuccess:@"修改成功"];
                    [Util setObject:self.pwdField.text key:@"pwd"];
                    [Util setBool:NO key:@"islogin"];
                    CHLoginViewController *loginVc = [[CHLoginViewController alloc] init];
                    UIWindow *window = [UIApplication sharedApplication].keyWindow;
                    window.rootViewController = loginVc;
                    
                    
                }else{
                    
                    [MBProgressHUD showTipMessageInView:result[@"redes"]];
                    
                }
                
            }
            
        }];
    }
}

- (IBAction)newBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    _pwdField.secureTextEntry =  !_pwdField.secureTextEntry;
}

- (IBAction)sureBtnClick:(UIButton *)sender {
    sender.selected = !sender.selected;
    _newpwdField.secureTextEntry =  !_newpwdField.secureTextEntry;
}




-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

@end
